<template>
  <view>
    <button @click="get">get请求测试</button>
    <button @click="post">post请求测试</button>
    <view class="registerBox">
      帐号：<input type="text" v-model="register_account">
      密码：<input type="safe-password" password="true" v-model="register_password">
      <button @click="register">注册</button>
    </view>
    <view class="loginBox">
      登录帐号：<input type="text" v-model="login_account">
      登录密码：<input type="safe-password" password="true" v-model="login_password">
      <button @click="login">登录</button>
    </view>
    <!-- #ifdef H5 -->
    <text class="col">在h5显示</text>
    <!-- #endif -->
    <!-- #ifdef MP-WEIXIN -->
    <text class="col">在vx小程序显示</text>
    <!-- #endif -->
  </view>
</template>

<script>
  import http from '../../utils/http.js'
  export default {
    data() {
      return {
        register_account: '',
        register_password: '',
        login_account: '',
        login_password: ''
      }
    },
    methods: {
      login() {
        http({
          url: '/login',
          method: 'post',
          data: {
            account: this.login_account,
            password: this.login_password
          },
          success(res) {
            if (res.data.msg == '登录成功') {
              uni.showToast({
                title: res.data.msg,
                icon: 'success'
              })
            } else {
              uni.showToast({
                title: res.data.msg,
                icon: 'error'
              })
            }
          }
        })
      },
      register() {
        http({
          url: `/register`,
          params: {
            account: this.register_account,
            password: this.register_password
          },
          success(res) {
            if (res.data.msg == '注册成功') {
              uni.showToast({
                title: res.data.msg,
                icon: 'success'
              })
            } else {
              uni.showToast({
                title: res.data.msg,
                icon: 'error'
              })
            }
          }
        })
      },
      get() {
        http({
          url: '/testget'
        })
      },
      post() {
        http({
          url: '/testpost',
          method: 'post',
          data: {
            b: 1,
            c: 2
          }
        })
      }
    }
  }
</script>

<style>
  .col {
    /* #ifdef H5 */
    color: red;
    /* #endif */

    /* #ifdef MP-WEIXIN */
    color: blue;
    /* #endif */

  }
</style>
